<?php

namespace App\Http\Controllers\Service\Util;

use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class TableHelperController extends ApiController
{
    public function maxIds(Request $request)
    {
        $extras = $request->extras
            ? explode(',', $this->trimSpace($request->extras))
            : [];

        $tables = array_merge([
            'assets',
            'brands',
            'products',
            'product_images',
            'product_pivot_tag',
            'pack_categories',
            'packs',
            'pack_pivot_asset',
            'pack_asset_pivot_product',
        ], $extras);

        foreach ($tables as $table) {
            /**
             * @see \Illuminate\Database\Schema\MySqlBuilder::hasTable
             */
            if (Schema::hasTable($table)) {
                $max_id = DB::table($table)->max('id');

                echo "$table => $max_id<br>";
            } else {
                echo "table: [$table] not exist<br>";
            }
        }
    }

    /**
     * @param string $string
     *
     * @return null|string|string[]
     */
    private function trimSpace(string $string): string
    {
        return preg_replace('/\s+/', '', $string);
    }
}
